<?php

class TypeController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
        //set session key

        $this->session_key = 'controller_type';
        $this->session_list_name = 'parent_id_list';
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'type', '');
    }

    public function viewAction() {
        if ($this->_getParam('child_id') && $this->_getParam('child_id') != "") {
            $child_id = $this->_getParam('child_id');
        } else {
            $this->_helper->redirector('list', 'type', '');
        }
        $typeModel = new Model_List();
        $typedata = $typeModel->fetchEntryByViewId($child_id);
        $updated_id = $typedata['lgn_id'];
        $login_type = $typedata['lgn_type'];

        /*         * get user name which is updated data* */
        $loginModel = new Model_Login();
        $updated_by_user = $loginModel->getUserData(Array('lgn_id' => $updated_id));
        $user_name = ucfirst($updated_by_user->name) . " [" . ucfirst($updated_by_user->role) . "]";

        $this->view->admindata = $user_name;
        $this->view->typedata = $typedata;
    }

    public function listAction() {
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $orderField = 'child_id';
        $sort = 'asc';
        //if(!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])){
        //  $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = "";
        // }

        /** Pagination Login * */
        $itemsPerPageReviews = $this->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        //PAGING_PER_PAGE
        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {

                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    $ids = implode(",", $_POST["multicheck"]);
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {

                    $errorMessage[] = $this->ZFTRANSLATE->translate("select_one_checkbox");
                }
            }
            $parent_list_id = $postedData["parent_id_list"];
            if ($parent_list_id != '') {
                $where = "child.parent_id = '" . $parent_list_id . "' ";
            }
            $_SESSION['_LISTING_SESS'][$this->session_key][$this->session_list_name] = $parent_list_id;
        } else {
            $parent_list_id = $_SESSION['_LISTING_SESS'][$this->session_key][$this->session_list_name];
            if ($parent_list_id != '') {
                $where = "child.parent_id = '" . $parent_list_id . "' ";
            }
            $_SESSION['_LISTING_SESS'][$this->session_key][$this->session_list_name] = $parent_list_id;
        }
        $typeModel = new Model_List();
        $typesData = $typeModel->fetchType($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        $parentdataArray = $typeModel->getparentArray();
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($typeModel->getTypeCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'type', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

       
        $this->view->parent_id_list = $_SESSION['_LISTING_SESS'][$this->session_key][$this->session_list_name];
        $this->view->paginator = $paginator;
        $this->view->typesData = $typesData;
        $this->view->parentdata = $parentdataArray;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->statusArray = $typeModel->getStatusArray();
        $this->view->searchtype = $searchtype;
    }

    public function deleteAction($ids = Array()) {
        //Load Type Model 
        $typeModel = new Model_List();
        if ($this->_getParam('child_id') && $this->_getParam('child_id') != "") {
            $child_id = $this->_getParam('child_id');
            $typeModel->deleteType($child_id);
            $this->addSuccessMessage($this->ZFTRANSLATE->translate("type_delete_success"));
            $this->_helper->redirector('list', 'type', '');
        } else {
            $typeModel->deleteType($ids);
            $this->addSuccessMessage($this->ZFTRANSLATE->translate("type_delete_success"));
            $this->_helper->redirector('list', 'type', '');
        }
    }

    public function formAction() {
        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $typeData = Array();
        $this->view->mode = "Add";

        //Load Type Model
        $typeModel = new Model_List();

        //Load Type Form
        $typeForm = new Form_TypeForm();

        //Load the parent  array for the select box in form 
        $parentArray = $typeModel->getparentArray();
        $typeForm->parent_id->addMultiOptions($parentArray);
        
        //$refchildArray = $typeModel->getrefchildArray();
        $typeForm->ref_child_id->addMultiOptions(array('' => '--Select Reference--'));

        
        //Load the language  array for the select box in form 
        $languageArray = $typeModel->getlanguageArray();
        $typeForm->lang_id->addMultiOptions($languageArray);

        //Load the status array for the select box in form 
        $statusArray = $typeModel->getStatusArray();
        $typeForm->child_status->addMultiOptions($statusArray);

        //Check for the type Parameter
        $typeData['child_id'] = $this->_request->getParam('child_id');
        $child_id = (!empty($typeData['child_id'])) ? $typeData['child_id'] : null;

        if (!empty($child_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $typeModel->fetchEntryById($child_id);
            $refchildArray = $typeModel->getrefchildArray($data['parent_id']);
            $typeForm->ref_child_id->addMultiOptions($refchildArray);

        }

        //Populate form 
        $this->view->form = $typeForm->populate($data);
        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
            
            if($postedData['parent_id'] != ''){
                $refchildArray = $typeModel->getrefchildArray($postedData['parent_id']);
                $typeForm->ref_child_id->clearMultiOptions();
                $typeForm->ref_child_id->addMultiOptions($refchildArray);
            }
            
            if (!$typeForm->isValid($postedData)) {
                $errorMessage = $typeForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {


                if (count($errorMessageArray) > 0) {
                    
                } else {

                    /*                     * authentication login id */
                    $defaultSession = new Zend_Session_Namespace(Zend_Registry::get('sessionName'));
                    $defaultSessionVal = $defaultSession->getIterator();
                    $updated_userid = $defaultSessionVal['storage']->lgn_id;

                    //Type save
                    $typeData['updated_lgn_id'] = $updated_userid;
                    $typeData['child_id'] = $child_id;
                    $typeData['child_name'] = $postedData['child_name'];
                    $typeData['ref_child_id'] = $postedData['ref_child_id'];
                    $typeData['parent_id'] = $postedData['parent_id'];
                    $typeData['child_status'] = $postedData['child_status'];
                    $typeData['lang_id'] = $postedData['lang_id'];


                    if ($this->view->mode == "Add") {
                        $typeData['child_createddate'] = date("Y-m-d H:i:s");
                    }
                    $typeData['child_updateddate'] = date("Y-m-d H:i:s");

                    $typeModel->save($typeData, 'child_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = $this->ZFTRANSLATE->translate("type_add_success");
                    } else {
                        $successmessage = $this->ZFTRANSLATE->translate("type_update_success");
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'type', '');
                }
            }
        }
        $this->view->mode = $this->view->mode;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $typeForm;
    }

     public function englishformAction() {
        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $typeData = Array();
        $this->view->mode = "Add";

        //Load Type Model
        $typeModel = new Model_List();

        //Load Type Form
        $typeForm = new Form_TypeenglishForm();

        //Load the parent  array for the select box in form 
        $parentArray = $typeModel->getparentArray();
        $typeForm->parent_id->addMultiOptions($parentArray);

        //Load the language  array for the select box in form 
        //$languageArray = $typeModel->getlanguageArray();
       // $typeForm->lang_id->addMultiOptions($languageArray);

        //Load the status array for the select box in form 
        $statusArray = $typeModel->getStatusArray();
        $typeForm->child_status->addMultiOptions($statusArray);

        //Check for the type Parameter
        $typeData['child_id'] = $this->_request->getParam('child_id');
        $child_id = (!empty($typeData['child_id'])) ? $typeData['child_id'] : null;

        if (!empty($child_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $typeModel->fetchEntryById($child_id);
        }

        //Populate form 
        $this->view->form = $typeForm->populate($data);
        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$typeForm->isValid($postedData)) {
                $errorMessage = $typeForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {


                if (count($errorMessageArray) > 0) {
                    
                } else {

                    /*                     * authentication login id */
                    $defaultSession = new Zend_Session_Namespace(Zend_Registry::get('sessionName'));
                    $defaultSessionVal = $defaultSession->getIterator();
                    $updated_userid = $defaultSessionVal['storage']->lgn_id;

                    //Type save
                    $typeData['updated_lgn_id'] = $updated_userid;
                    $typeData['child_id'] = $child_id;
                    $typeData['child_name'] = $postedData['child_name'];
                    $typeData['parent_id'] = $postedData['parent_id'];
                    $typeData['child_status'] = $postedData['child_status'];
                    $typeData['lang_id'] = 17;


                    if ($this->view->mode == "Add") {
                        $typeData['child_createddate'] = date("Y-m-d H:i:s");
                    }
                    $typeData['child_updateddate'] = date("Y-m-d H:i:s");

                    $dropdownid = $typeModel->save($typeData, 'child_id');
                    $typeData['child_id'] = $dropdownid;
                    $typeData['ref_child_id'] = $dropdownid;
                    
                    $typeModel->save($typeData, 'child_id');
                    
                    if ($this->view->mode == "Add") {
                        $successmessage = $this->ZFTRANSLATE->translate("type_add_success");
                    } else {
                        $successmessage = $this->ZFTRANSLATE->translate("type_update_success");
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'type', '');
                }
            }
        }
        $this->view->mode = $this->view->mode;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $typeForm;
    }

       public function referencelistAction(){
        $listString = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            $typeModel =new Model_List();
            $parent_id = $postedData['parent_id'];
            $refchildArray = $typeModel->getrefchildArray($parent_id);
           // $typeForm->ref_child_id->addMultiOptions($refchildArray);
            foreach($refchildArray as $key => $_cont) {
                    //print_r($_cont);
                    $listString .= "<option value = '".$key ."'>".$_cont."</option>";
                }
           
            
        }
      echo $listString;
      exit;
    } 

  
}

?>
